﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Models;
using AnesSystem.ViewModels;
using Common;
using System.Windows;
using AnesSystem.Controls;
using MongoDB.Bson;
using AnesSystem.Services;
using System.Xml;
using System.Windows.Controls;

namespace AnesSystem
{
    public class NavigateManager : BaseManager<NavigateManager>
    {
        #region Variables

        Dictionary<ObjectId, CommonPageContent> _dicContent = new Dictionary<ObjectId, CommonPageContent>();

        CommonPage _currentPage;

        Dictionary<ObjectId, CommonPage> _dicPage = new Dictionary<ObjectId, CommonPage>();

        #endregion

        #region Constructor

        public NavigateManager()
        {
        }

        #endregion

        #region Properties

        public CommonPage Page
        {
            get
            {
                return _currentPage;
            }
            set
            {
                _currentPage = value;
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void Goto(MenuListModel menu, bool isMenuClick)
        {
            ObjectId key;
            if (menu.ClassName.Contains("AnesRecordCtrl"))
            {
                MenuListModel pMenu = DataManager.Instance.MenuList.First(p => p.Index == menu.Parent);
                FirstLevelMenuListCtrlViewModel.Instance.SelectedMenu = DataManager.Instance.MenuList.First(O => O.Index == pMenu.Parent);
                key = FirstLevelMenuListCtrlViewModel.Instance.SelectedMenu.Id;
            }
            else
            {
                key = menu.Id;
            }
            CommonPage page;
            if (_dicPage.ContainsKey(key))
            {
                page = _dicPage[key];
            }
            else
            {
                page = new CommonPage(menu);
                page.PageName = key.ToString();
                _dicPage.Add(key, page);
            }
            if (_currentPage == null)
            {
                NavigateWin.Navigation.Navigate(page);
            }
            else if (_currentPage != null)
            {
                if (_currentPage.PageName.Equals(page.PageName) && menu.MenuType != 0)
                {
                    page.ChangeContent(DataManager.Instance.MenuList.First(p => p.Index == menu.Parent));
                }
                else if (menu.MenuType != 0)
                {
                    page.ChangeContent(DataManager.Instance.MenuList.First(p => p.Index == menu.Parent));
                    NavigateWin.Navigation.Navigate(page);
                }
                else
                {
                    NavigateWin.Navigation.Navigate(page);
                }
            }
            _currentPage = page;
        }


        #endregion

        #region Public/Protected Methods

        public void NavigateTo(MenuListModel model, bool isMenuClick = true)
        {
            Goto(model, isMenuClick);
        }

        public void NavigateTo(string pageName, bool isMenuClick = true)
        {
            MenuListModel model = DataManager.Instance.MenuList.First(O => O.ClassName.Contains(pageName));
            Goto(model, isMenuClick);
        }

        #endregion

        #region EventHandlers

        #endregion
    }
}
